package cn.lrwjz.androidguide.ui;

import android.os.Bundle;
import android.view.MenuItem;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.android.material.search.SearchBar;
import com.google.android.material.search.SearchView;
import com.google.android.material.search.SearchView.TransitionState;

import java.util.ArrayList;
import java.util.List;

import cn.lrwjz.androidguide.R;
import cn.lrwjz.androidguide.adapter.SearchAdapter;

public class SearchActivity extends AppCompatActivity {
    private SearchBar searchBar;
    private SearchView searchView;
    private RecyclerView searchResultsList;
    private SearchAdapter searchAdapter;
    private List<String> suggestions;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);

        // 初始化搜索建议数据
        suggestions = new ArrayList<>();
        suggestions.add("Material Design");
        suggestions.add("Android");
        suggestions.add("Kotlin");
        suggestions.add("Java");
        suggestions.add("Flutter");
        suggestions.add("React Native");

        // 初始化视图
        searchBar = findViewById(R.id.searchBar);
        searchView = findViewById(R.id.searchView);
        searchResultsList = findViewById(R.id.searchResultsList);

        // 设置 RecyclerView
        searchResultsList.setLayoutManager(new LinearLayoutManager(this));
        searchAdapter = new SearchAdapter(suggestions);
        searchResultsList.setAdapter(searchAdapter);

        // 设置搜索监听器
        searchView.getEditText().setOnEditorActionListener((v, actionId, event) -> {
            String query = searchView.getText().toString();
            performSearch(query);
            return true;
        });

        // 设置返回按钮点击事件
        searchBar.setNavigationOnClickListener(v -> finish());

        // 设置搜索视图的状态监听
        searchView.addTransitionListener((searchView, previousState, newState) -> {
            if (newState == TransitionState.HIDDEN) {
                // 搜索视图隐藏时的处理
                searchBar.setText(searchView.getText());
            }
        });

        // 设置建议项点击事件
        searchAdapter.setOnItemClickListener(suggestion -> {
            searchView.setText(suggestion);
            performSearch(suggestion);
        });

        // 将 SearchView 与 SearchBar 关联
        searchView.setupWithSearchBar(searchBar);
    }

    private void performSearch(String query) {
        // 这里实现实际的搜索逻辑
        Toast.makeText(this, "搜索: " + query, Toast.LENGTH_SHORT).show();
        
        // 模拟搜索结果
        List<String> results = new ArrayList<>();
        for (String suggestion : suggestions) {
            if (suggestion.toLowerCase().contains(query.toLowerCase())) {
                results.add(suggestion);
            }
        }
        searchAdapter.updateData(results);
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
} 